﻿#region Copyright (C) 2008, 2009 StreamTv

/* 
 *      Copyright (C) 2008, 2009 StreamTv
 *      http://code.google.com/p/mpstreamtv/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using TimeShifting.Media.Encoders;
using System.Diagnostics;

namespace TimeShifting.Media.Encoders
{
    public class FFMpeg : ProcessWrapper
    {
        private Process applicationThread;
        private ProcessStartInfo applicationDetails;
        private TranscodeOptions settings;

        public FFMpeg(Stream media, TranscodeOptions options) :
            base(media, TransportMethod.NamedPipe, TransportMethod.NamedPipe)
        {
            settings = options;

            base.Start();
        }

        public FFMpeg(String url, TranscodeOptions options) :
            base(url, TransportMethod.NamedPipe, TransportMethod.NamedPipe)
        {
            settings = options;

            base.Start();
        }

        public override void StartProcess(String input, String output)
        {
            if (applicationThread != null)
                applicationThread.Kill();

            applicationDetails = new ProcessStartInfo(String.Format(@"{0}\Team MediaPortal\MediaPortal TV Server\StreamTv\FFMpeg\bin\ffmpeg.exe", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)), String.Format(settings.Parameters, input, output));

            applicationDetails.UseShellExecute = false;
            //applicationDetails.RedirectStandardOutput = true;
            //StreamReader reader;
            
            applicationThread = new Process();

            applicationThread.StartInfo = applicationDetails;
            applicationThread.Start();

            //reader = applicationThread.StandardOutput;

            //System.Diagnostics.Debug.Write(reader.ReadToEnd());
        }

        public override void StopProcess()
        {
            if (applicationThread != null)
            {
                applicationThread.Kill();
            }
        }
    }
}
